var ageReg = /^[1-9]\d*$/;
var nameReg= /^(([\u4e00-\u9fa5]{2,8})|([a-zA-Z]{2,16}))$/;
var careerReg= /^(([\u4e00-\u9fa5]{2,8})|([a-zA-Z]{2,16}))$/;

var $age = $("#age");
var $name = $("#name");
var $career = $("#career");

var flagage = true;
var flagname = true;
var flagcareer = true;

(function(){
	
    $("#sex").val($("#sexSelect").val());

    $("#submitBtn").on("click", submit);

	   //年龄输入框事件
    $age.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var age = $.trim($this.val());

      flagage = validateForm($this, age, ageReg, "请输入正确的年龄");
    });
	
	   //姓名输入框事件
    $name.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var name = $.trim($this.val());

      flagname = validateForm($this, name, nameReg, "请输入正确的姓名");
    });
	
	   //职业输入框事件
    $career.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var career = $.trim($this.val());

      flagcareer = validateForm($this, career, careerReg, "请输入正确的职业");
    });
	
	   //表单验证, 前面四个都是必传参数：当前输入框、当前值、正则、提示文字，最后一个参数是可选参数：查看明文密码
    function validateForm(that, value, reg, txt, cleartext){
      var $parent = that.closest(".form-group");
      var $addon = $parent.find(".input-group-addon");
      var $msg = $parent.find(".js-line-alert");
      var $msgText = $parent.find(".js-alert-text");
      
      var flag = true;

      if(value.length != 0){
        if(!reg.test(value)){
          modifyWarning($msgText, txt);
          showWarning($addon, that, $msg, cleartext);
          flag = false;
        }
        else{
          hideWarning($addon, that, $msg, cleartext);
        }
      }
      
      return flag;
    }
    
    //输入框边框变红并显示提示文字
    function showWarning(addon, input, msg, cleartext){
        if(cleartext){
          cleartext.addClass("input-error border-error");
        }
        addon.addClass("border-error");
        input.addClass("input-error border-error");
        msg.show();
    }
    
  	//输入框边框恢复原色并隐藏提示文字
    function hideWarning(addon, input, msg, cleartext){
        if(cleartext){
          cleartext.removeClass("input-error border-error");
        }
        addon.removeClass("border-error");
        input.removeClass("input-error border-error");
        msg.hide();
    }
    
  	//修改提示文字信息
    function modifyWarning(element, txt){
        element.text(txt);
    }

		//表单登录提交
    function submit(){
			var age = $.trim($age.val());
			var name = $.trim($name.val());
			var career = $.trim($("#career").val());
		  var sex = $("#sex").val();
		  var memberId = $("#memberId").val();

      $age.blur();
      $name.blur();
      $career.blur();
		
			if(flagage && flagname && flagcareer){
				$.ajax({
                	url: "upInfor",
                	type: "post",
                	dataType: 'json',
                	data : {
					memberId:memberId,
					age:age,
                		name:name,
                		career:career,
					sex:sex
                	},
                	success: function(data) {
                		if (data.returnFlag == '0') {
                            layer.open({
                                content: '修改信息成功！',
                                scrollbar: false
                            });
                			location.href = "../user/myPc";
											//location.reload();
                		}else {
                            layer.open({
                                content: '验证失败!',
                                scrollbar: false
                            });
                		}
                	},
                	error: function(xhr, type, errorThrown) {
                		//异常处理；
						alert("请稍后再试!")
                	}
    			});
			}
    }
	
})();